<script setup lang="ts">
import { areaDelApi, areaGetListApi } from '@/apis/card'
import type { ListParams } from '@/types'
import type { AreaI } from '@/types/card'
import { ElMessage } from 'element-plus'
import { ref } from 'vue'
import AreaDialog from './components/AreaDialog.vue'

const listParams = ref<ListParams>({
  page: 1,
  pageSize: 8,
})
const list = ref<AreaI[]>([])
const total = ref(0)
const getList = async () => {
  const res = await areaGetListApi(listParams.value)
  list.value = res.rows
  total.value = res.total
}
getList()
const changePage = (page: number) => {
  listParams.value.page = page
  getList()
}
const changePageSize = (pageSize: number) => {
  listParams.value.page = 1
  listParams.value.pageSize = pageSize
  getList()
}
const onDel = async (id: number) => {
  await areaDelApi(id)
  getList()
  ElMessage.success('删除成功~')
}
/**
 * 弹层
 */
const dialogVisible = ref(false)
const dialogParams = ref<AreaI>({
  id: undefined,
  areaName: '',
  spaceNumber: undefined,
  areaProportion: undefined,
  remark: '',
  ruleId: undefined,
})
const onEdit = (row: AreaI) => {
  dialogVisible.value = true
  const { id, areaName, spaceNumber, areaProportion, remark, ruleId } = row
  dialogParams.value = {
    id,
    areaName,
    spaceNumber,
    areaProportion,
    remark,
    ruleId,
  }
}
</script>

<template>
  <div class="page-container">
    <div class="header-container">
      <el-button type="primary" @click="dialogVisible = true"
        >添加区域</el-button
      >
    </div>
    <!-- 表格 -->
    <el-table :data="list">
      <el-table-column label="序号" type="index" width="80" align="center" />
      <el-table-column label="区域名称" align="center" prop="areaName" />
      <el-table-column label="车位数(个)" align="center" prop="spaceNumber" />
      <el-table-column label="面积(m²)" align="center" prop="areaProportion" />
      <el-table-column label="计费规则" align="center" prop="ruleName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button type="text" @click="onEdit(row)">编辑</el-button>
          <el-popconfirm title="确认删除吗?" @confirm="onDel(row.id)">
            <template #reference>
              <el-button type="text">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      v-model:current-page="listParams.page"
      v-model:page-size="listParams.pageSize"
      layout="total, prev, pager, next, slot, jumper"
      :total="total"
      @current-change="changePage"
    >
      <el-select
        v-model="listParams.pageSize"
        style="width: 95px"
        @change="changePageSize"
      >
        <el-option label="8条/页" :value="8" />
        <el-option label="10条/页" :value="10" />
        <el-option label="15条/页" :value="15" />
      </el-select>
    </el-pagination>
    <!-- 弹层 -->
    <area-dialog
      v-model:dialogVisible="dialogVisible"
      v-model:dialogParams="dialogParams"
      @success="getList"
    />
  </div>
</template>

<style lang="scss" scoped>
.page-container {
  padding: 20px;

  .el-table {
    margin: 20px 0;
    height: 400px;

    ::v-deep(.el-table__header th) {
      background-color: rgb(237, 237, 237, 0.9);
      color: rgb(43, 37, 37);
      padding: 10px 0;
    }
  }

  .el-pagination {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
